from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.Qt import *
from PyQt5.QtGui import QCursor
import sys, time, random
from datetime import datetime


class Game_Base_UI(QtWidgets.QMainWindow):
    # __slots__ = self.player_name,self.Player_Lev,self.Player_gengu,self.Player_linggen,self.player_xiuwei,self.Player_naqi,self.test_button   
    def __init__(self):
        super().__init__()
        self.Player_name = '二狗'  # 玩家名称
        self.Player_Lev = int(0)  # 玩家初始等级
        self.Player_gengu = int(10)  # 玩家初始属性--根骨
        self.Player_linggen = int(10)  # 玩家初始属性--灵根
        self.Player_xiuwei = int(0)  # 玩家当前经验与升级所需经验
        self.Player_naqi = int(100)  # 玩家纳气值及每秒获取经验值
        self.Player_xiuwei_max = int(100)  # 玩家经验升级所需经验初始值100

        self.init_ui()  # 运行init_ui创建base_win
        self.n = int(0)

        self.Game_status()
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.Game_status)  # 不断访问Game_status函数
        timer.start(1000)  # 访问间隔时间1000==1秒

    def init_ui(self):
        self.setFixedSize(1000, 700)
        self.main_widget = QtWidgets.QWidget()  # 创建窗口主部件
        self.main_layout = QtWidgets.QGridLayout()  # 创建主部件的网格布局
        self.main_widget.setLayout(self.main_layout)  # 设置窗口主部件布局为网格布局
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground) #self.main 主窗口Background隐藏
        self.setCentralWidget(self.main_widget)  # 设置窗口主部件
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)  # 隐藏边框
        self.main_widget.setObjectName('main_widget')

        self.init_left_ui()
        self.init_middle_ui()
        self.init_right_ui()
        self.main_second_UI()

        self.main_layout.setSpacing(0)
        self.main_widget.setStyleSheet('''QToolButton{border:none;}''')

        ####↓↓↓↓↓↓↓↓图标测试↓↓↓↓↓↓↓↓↓####
        self.test_button = QtWidgets.QToolButton()
        self.test_button.setText(str(''))
        self.test_button.setIcon(QIcon('./icon/m.png'))
        self.test_button.setIconSize(QSize(50, 50))
        self.test_button.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
        self.test_button.setFont(QtGui.QFont('SansSerif', 15))
        self.test_button.setToolTip('退隐江湖')
        self.test_button.setObjectName('test_Button')
        self.main_layout.addWidget(self.test_button, 34, 47, 1, 1)
        self.main_widget.setStyleSheet('''QToolButton#test_Button{border:none;}''')
        self.test_button.clicked.connect(self.close)
        ####↑↑↑↑↑↑↑↑↑图标测试↑↑↑↑↑↑↑↑↑####


    def main_second_UI(self):               #在main_win上增加蒙层 取消button按下功能
        self.main_second_widget =QtWidgets.QWidget()
        self.main_second_layout=QtWidgets.QGridLayout()
        self.main_second_widget.setLayout(self.main_second_layout)
        self.main_second_widget.setAttribute(QtCore.Qt.WA_TranslucentBackground)    #隐藏蒙层背景
        self.main_layout.addWidget(self.main_second_widget,0,0,50,51)

    def init_left_ui(self):                 #创建左侧画轴卷
        self.main_left_button = QtWidgets.QToolButton()
        self.main_left_button.setObjectName('main_label')
        self.main_left_button.setIcon(QIcon('./image/Game_png_1.png'))
        self.main_left_button.setIconSize(QSize(161, 495))
        self.main_left_button.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
        self.main_layout.addWidget(self.main_left_button,0,0,50,4)

    def init_middle_ui(self):               #创建中间画轴
        self.main_middle_label = QtWidgets.QLabel()
        self.pix=QPixmap('./image/Game_png_2.png')
        self.main_middle_label.setPixmap(self.pix)
        self.main_middle_label.setScaledContents(True)
        self.main_layout.addWidget(self.main_middle_label,10,3,25,46)

    def init_right_ui(self):                #创建右侧画轴卷
        self.main_right_button = QtWidgets.QToolButton()
        self.main_right_button.setObjectName('main_label')
        self.main_right_button.setIcon(QIcon('./image/Game_png_3.png'))
        self.main_right_button.setIconSize(QSize(160, 495))
        self.main_right_button.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
        self.main_layout.addWidget(self.main_right_button,0,48,50,4)


    def All_Num_random(self):               #随机生成数值
        return random.randrange(1, 11) / 10

    def Game_status(self):
        
        if self.Player_Lev == 0:
            self.player_update_mes = QtWidgets.QLabel('')                                                       # 如果玩家等级为0初始消息为空
            self.player_update_mes.setObjectName('update_mes_Label')                                            # 将消息别名设置

        try:
            self.player_name.deleteLater()                                                                      # 尝试删除已存在的label
            self.player_Lev.deleteLater()
            self.player_gengu.deleteLater()
            self.player_linggen.deleteLater()
            self.player_xiuwei.deleteLater()
            self.player_naqi.deleteLater()

        except:
            pass
        if self.Player_xiuwei >= self.Player_xiuwei_max:                                                        # 如果当前修为等于升级所需修为执行升级函数
            self.player_update_mes.deleteLater()                                                                # 删除已存在的消息label
            self.player_update_mes = QtWidgets.QLabel('%s   %s升级! %s-->%s,属性得到了提升!' % (time.strftime("%d %H:%M", time.localtime()) ,self.Player_name, self.Player_Lev, self.Player_Lev + 1))    # 创建新的消息label
            self.player_update_mes.setObjectName('update_mes_Label')
            self.Player_xiuwei -= self.Player_xiuwei_max                                                        # 新的当前修为等于升级前当前修为减去升级所需修为
            self.Player_Lev += 1                                                                                # 等级+1
            self.Player_naqi += 1                                                                               # 每秒的纳气值+1
            self.Player_xiuwei_max += 100                                                                       # 升级所需的修为+100
            self.Player_gengu += self.All_Num_random()                                                          # 执行随机生成值
            self.Player_linggen += self.All_Num_random()

        self.player_name = QtWidgets.QLabel('名称:%s' % self.Player_name)
        self.player_name.setObjectName('Status_Label')
        self.player_Lev = QtWidgets.QLabel('等级:%s' % self.Player_Lev)
        self.player_Lev.setObjectName('Status_Label')
        self.player_gengu = QtWidgets.QLabel('根骨:%.1f' % self.Player_gengu)
        self.player_gengu.setObjectName('Status_Label')
        self.player_linggen = QtWidgets.QLabel('灵根:%.1f' % self.Player_linggen)
        self.player_linggen.setObjectName('Status_Label')
        self.player_xiuwei = QtWidgets.QLabel('修为:%s/%s' % (self.Player_xiuwei, self.Player_xiuwei_max))
        self.player_xiuwei.setObjectName('Status_Label')
        self.player_naqi = QtWidgets.QLabel('纳气:%s/秒' % self.Player_naqi)
        self.player_naqi.setObjectName('Status_Label')

        self.main_layout.addWidget(self.player_name, 15, 7, 1, 4, Qt.AlignLeft)
        self.main_layout.addWidget(self.player_Lev, 17, 7, 1, 4, Qt.AlignLeft)
        self.main_layout.addWidget(self.player_gengu, 19, 7, 1, 4, Qt.AlignLeft)
        self.main_layout.addWidget(self.player_linggen, 21, 7, 1, 4, Qt.AlignLeft)
        self.main_layout.addWidget(self.player_xiuwei, 23, 7, 1, 4, Qt.AlignLeft)
        self.main_layout.addWidget(self.player_naqi, 25, 7, 1, 4, Qt.AlignLeft)
        self.main_layout.addWidget(self.player_update_mes, 33, 7, 1, 18, Qt.AlignLeft)
        self.Player_xiuwei += self.Player_naqi


    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()                                                    # 获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))                                                          # 更改鼠标图标

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)                                                # 更改窗口位置

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))


def main():
    app = QtWidgets.QApplication(sys.argv)
    gui = Game_Base_UI()
    gui.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
